package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.mybatis.Pagination;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.work.WorkBO;
import com.ymk.bo.work.WorkCollectBO;
import com.ymk.core.code.cmd.WorkCollectCmdCode;
import com.ymk.core.constants.WxConstants;
import com.ymk.service.IWorkCollectService;
import com.ymk.service.IWorkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class WorkCollectListCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        WorkCollectBO reqData = super.getData(sign, WorkCollectBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("userId",reqData.getUserId())
            .setReturnStruct(sign.getReturnStruct());
        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<WorkCollectBO> respDataList = this.WorkCollectService.listForPage(pagination);
        List<Integer> workIdList = new ArrayList<>();
        respDataList.forEach(WorkCollectBO ->{
            workIdList.add(WorkCollectBO.getWorkId());
        });
        List<WorkBO> workBOList = new ArrayList<>();
        if(!workIdList.isEmpty()){
            paramsBuilder.clearParam()
                    .addParam("loadWorkImg",true)
                    .addParam("loadWorkType",true)
                    .addParam("workIdList",workIdList);

            workBOList = workService.listForPage(paramsBuilder.build());
        }


        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(workBOList));

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return WorkCollectCmdCode.WORK_COLLECT_LIST;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        WorkCollectBO reqData = super.getData(sign, WorkCollectBO.class);
        String openId = request.getHeader(WxConstants.X_WX_OPENID);
        if (StringUtils.isBlank(openId)) {
            throw new MallException("请求异常,header未发现openID");
        }
        if(StringUtils.isBlank(reqData.getUserId())){
            throw new MallException("用户id不能为空");
        }
        return null;
    }


    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }
    @Autowired
    private IWorkCollectService WorkCollectService;
    @Autowired
    private IWorkService workService;
}